const express = require('express')
const router =express.Router()

const BlogModel = require('../models/blog')
const CommentModel = require('../models/comment')

router.get('/find',(req,res,next)=>{
    const {blog_id,user_id} = req.query
    if(blog_id!=''){
        BlogModel.aggregate([
            {
              $match: {blog_id:parseInt(blog_id)} 
            },
            {
              $lookup: {
                from: 'users',
                localField: 'user_id',
                foreignField: 'user_id',
                as: 'user_data',
              }
            },
            {
              $lookup: {
                from: 'comments',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'comment_data',
              }
            },
            {
              $lookup: {
                from: 'collections',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'collections_data',
              }
            },
            {
              $lookup: {
                from: 'nices',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'nices_data',
              }
            },
            {
              $lookup: {
                from: 'follows',
                localField: 'user_id',
                foreignField: 'followed_id',
                as: 'follows_data',
              }
            },
          ]).then(result=>{
        res.json({
            blog_id,
            code:200,
            data:result,
            message:'blog_id查询成功',
        })
    })}
    else if(user_id!=''){
        BlogModel.find({user_id}).then(result=>{
        res.json({
            code:200,
            data:result,
            message:'user_id查询成功',
        })
    })}
    else{
        BlogModel.aggregate([
            {
              $match: {} 
            },
            {
              $lookup: {
                from: 'users',
                localField: 'user_id',
                foreignField: 'user_id',
                as: 'user_data',
              }
            },
            {
              $lookup: {
                from: 'sortBlogs',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'sortBlog',
              }
            },
            {
              $lookup: {
                from: 'comments',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'comment_data',
              }
            },
            {
              $lookup: {
                from: 'collections',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'collections_data',
              }
            },
            {
              $lookup: {
                from: 'nices',
                localField: 'blog_id',
                foreignField: 'blog_id',
                as: 'nices_data',
              }
            },
            {
              $lookup: {
                from: 'follows',
                localField: 'user_id',
                foreignField: 'followed_id',
                as: 'follows_data',
              }
            },
          ]).sort({'blog_id':-1}).then(result=>{
        res.json({
            code:200,
            data:result,
            message:'查询成功',
        })
    })}
})

router.get('/findByTitle',(req,res,next)=>{
    const {title} = req.query
    const reg = '.*?'+title+'.*'
    BlogModel.aggregate([
        {
          $match: {} 
        },
        {
          $lookup: {
            from: 'users',
            localField: 'user_id',
            foreignField: 'user_id',
            as: 'user_data',
          }
        },
      ]).then(result=>{
        const list = []
        result.map(item => {
            if(item.title.match(reg)/* ||item.content.match(reg) */)list.push(item)
        })
        console.log(list)
        res.json({
            code:200,
            data:list,
            message:'title查询成功',
        })
    })
})

router.post('/create',(req,res,next)=>{
    const {title,content,html,user_id} = req.body

    BlogModel.find().then(result=>{
        const blog = new BlogModel({
            blog_id:result.length?result[result.length-1].blog_id+1:0,
            title,
            content,
            html,
            user_id,
            view:0,
            time:new Date()
        })
        console.log(blog)
        blog.save().then(result => {
            res.json({
                code:200,
                data:result,
                message:'创建完成',
            })
        })
    })
})

router.post('/update',(req,res,next)=>{
  const {blog_id,title,content,html} = req.body

    BlogModel.updateOne({blog_id},{$set:{"title":title,"content":content,"html":html}}).then(result=>{
        res.json({
            code:200,
            data:{
                result
            },
            message:'修改完成',
        })
    })
})

router.get('/view',(req,res,next)=>{
  const {blog_id} = req.query 
  BlogModel.find({blog_id}).then(result=>{
      BlogModel.updateOne({blog_id},{$set:{"view":result[0].view+1}}).then(result=>{
        res.json({
            code:200,
            data:{
                result
            },
            message:'访问成功',
        })
      })
  })
})

router.delete('/delete',(req,res,next)=>{
    const {blog_id} = req.body

    BlogModel.deleteOne({blog_id}).then(result=>{
      CommentModel.deleteMany({blog_id}).then(result=>{
        res.json({
            code:200,
            data:{
                result
            },
            message:'评论删除完成',
        })
      })
    })
})
module.exports = router